package com.hn.common.utils.ding;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.taobao.api.ApiException;

import java.util.List;

/**
 * 钉钉教育
 * @author admin
 */
public class DingEdu {

    /**
     *  <a href="https://ding-doc.dingtalk.com/doc#/serverapi3/gga05a/TZKti">查询校区列表</a>
     */
    public static List<OapiEduCampusListResponse.CampusResponse> listCampus()throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/campus/list");
        OapiEduCampusListRequest req = new OapiEduCampusListRequest();
        OapiEduCampusListResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        System.out.println(rsp.getBody());
        return rsp.getResult();
    }

    /**
     *  <a href="https://ding-doc.dingtalk.com/doc#/serverapi3/gga05a/80UxR">查询校区信息</a>
     */
    public static void getCampus(Long campusId)throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/campus/get");
        OapiEduCampusGetRequest req = new OapiEduCampusGetRequest();
        req.setCampusId(campusId);
        OapiEduCampusGetResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        System.out.println(rsp.getBody());
    }

    /**
     *  查询学段列表
     */
    public static List<OapiEduPeriodListResponse.PeriodResponse> listPeriod(Long campusId)throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/period/list");
        OapiEduPeriodListRequest req = new OapiEduPeriodListRequest();
        req.setCampusId(campusId);
        OapiEduPeriodListResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        System.out.println(rsp.getBody());
        return rsp.getResult();
    }

    /**
     *  查询学段信息
     */
    public static OapiEduPeriodGetResponse.PeriodResponse getPeriod(Long periodId)throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/period/get");
        OapiEduPeriodGetRequest req = new OapiEduPeriodGetRequest();
        req.setPeriodId(periodId);
        OapiEduPeriodGetResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        System.out.println(rsp.getBody());
        return rsp.getResult();
    }

    /**
     *  查询年级列表
     */
    public static List<OapiEduGradeListResponse.GradeResponse> listGrade(Long periodId)throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/grade/list");
        OapiEduGradeListRequest req = new OapiEduGradeListRequest();
        req.setPeriodId(periodId);
        OapiEduGradeListResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        System.out.println(rsp.getBody());
        return rsp.getResult();
    }

    /**
     *  查询年级信息
     */
    public static OapiEduGradeGetResponse.GradeResponse getGrade(Long gradeId)throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/grade/get");
        OapiEduGradeGetRequest req = new OapiEduGradeGetRequest();
        req.setGradeId(gradeId);
        OapiEduGradeGetResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        System.out.println(rsp.getBody());
        OapiEduGradeGetResponse.GradeResponse result = rsp.getResult();
        return result;
    }

    /**
     *  查询班级列表
     */
    public static OapiEduClassListResponse.PageResult listClass(Long gradeId)throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/class/list");
        OapiEduClassListRequest req = new OapiEduClassListRequest();
        req.setGradeId(gradeId);
        req.setPageNo(1L);
        req.setPageSize(20L);
        OapiEduClassListResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        System.out.println(rsp.getBody());
        OapiEduClassListResponse.PageResult result = rsp.getResult();
        return result;
    }

    /**
     *  查询班级信息
     */
    public static OapiEduClassGetResponse.ClassResponse getClass(Long classId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/class/get");
        OapiEduClassGetRequest req = new OapiEduClassGetRequest();
        req.setClassId(classId);
        OapiEduClassGetResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        System.out.println(rsp.getBody());
        OapiEduClassGetResponse.ClassResponse result = rsp.getResult();
        return result;
    }

    /* ----------------------------------- 角色信息 -----------------------------------------------------------------*/

    /**
     * 学生列表查询
     */
    public static OapiEduStudentListResponse.PageResult listStudent(Long classId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/student/list");
        OapiEduStudentListRequest req = new OapiEduStudentListRequest();
        req.setClassId(classId);
        req.setPageNo(1L);
        req.setPageSize(20L);
        OapiEduStudentListResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        System.out.println(rsp.getBody());
        OapiEduStudentListResponse.PageResult result = rsp.getResult();
        return result;
    }

    /**
     * 学生信息查询
     */
    public static OapiEduStudentGetResponse.StudentRespone getStudent(Long classId,String studentUserId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/student/get");
        OapiEduStudentGetRequest req = new OapiEduStudentGetRequest();
        req.setClassId(classId);
        req.setStudentUserid(studentUserId);
        OapiEduStudentGetResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        System.out.println(rsp.getBody());
        OapiEduStudentGetResponse.StudentRespone result = rsp.getResult();
        return result;
    }

    /**
     * 老师列表查询
     */
    public static List<OapiEduTeacherListResponse.TeacherRespone> listTeacher(Long classId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/teacher/list");
        OapiEduTeacherListRequest req = new OapiEduTeacherListRequest();
        req.setClassId(classId);
        OapiEduTeacherListResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        System.out.println(rsp.getBody());
        List<OapiEduTeacherListResponse.TeacherRespone> result = rsp.getResult();
        return result;
    }

    /**
     * 老师信息查询
     */
    public static OapiEduTeacherGetResponse.TeacherRespone getTeacher(Long classId,String teacherUserId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/teacher/get");
        OapiEduTeacherGetRequest req = new OapiEduTeacherGetRequest();
        req.setClassId(classId);
        req.setTeacherUserid(teacherUserId);
        OapiEduTeacherGetResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        System.out.println(rsp.getBody());
        OapiEduTeacherGetResponse.TeacherRespone result = rsp.getResult();
        return result;
    }

    /**
     * 家长列表查询
     * */
    public static OapiEduGuardianListResponse.PageResult listGuardian(Long classId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/guardian/list");
        OapiEduGuardianListRequest req = new OapiEduGuardianListRequest();
        req.setClassId(classId);
        req.setPageNo(1L);
        req.setPageSize(10L);
        OapiEduGuardianListResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        System.out.println(rsp.getBody());
        OapiEduGuardianListResponse.PageResult result = rsp.getResult();
        return result;
    }

    /**
     * 家长信息查询
     */
    public static OapiEduGuardianGetResponse.GuardianRespone getGuardian(Long classId,String guardianId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/guardian/get");
        OapiEduGuardianGetRequest req = new OapiEduGuardianGetRequest();
        req.setClassId(classId);
        req.setGuardianUserid(guardianId);
        OapiEduGuardianGetResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        System.out.println(rsp.getBody());
        OapiEduGuardianGetResponse.GuardianRespone result = rsp.getResult();
        return result;
    }

    /**
     * 用户角色查询
     */
    public static OapiEduRolesGetResponse.QueryUserRolesResponse listRolesByUserId(String userId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/roles/get");
        OapiEduRolesGetRequest req = new OapiEduRolesGetRequest();
        req.setUserid(userId);
        OapiEduRolesGetResponse rsp = client.execute(req, AccessTokenUtil.getToken());
        System.out.println(rsp.getBody());
        OapiEduRolesGetResponse.QueryUserRolesResponse result = rsp.getResult();
        return result;
    }

    /**
     * 钉钉云数据推送
     * 当 biz_type = 25时，数据为家校通讯录数据。在使用前，需要申请“企业通讯录权限” 和 “教育行业扩展通讯录读权限”，
     * 在开发者后台选择企业通讯录相关权限，并勾选“家校人员变更” 和 “家校通讯录变更” 选项。
     */

    /**
     * 角色：
     *  学生
     *  老师[班主任、非班主任]
     *  家长
     *
     * 学校信息：
     *  校区
     *    -- 学段 幼儿园：kindergarten； 小学：primary_school； 初中：middle_school；高中：high_school
     *      -- 年级 grade_level：年级级数（一年级为1，二年级为2）
     *       -- 班级 class_level: 班级级数 1班为1,2班为2
     */
    public static void main(String[] args)throws Exception {
        // 1209169
//        listCampus(); // 348531711
//        listPeriod(348531711L);
//        listGrade(348560709L);
//        listClass(348578829L);

//        listTeacher(348738494L);

//        getTeacher();
//        listStudent(348738494L);
//        listGuardian(348738494L);
//        listRolesByUserId("68506659759559");


         /* DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/campus/list");
        OapiEduCampusListRequest req = new OapiEduCampusListRequest();
        OapiEduCampusListResponse rsp = client.execute(req, access_token);
        System.out.println(rsp.getBody());*/
/*
        SimpleDataSource ds = new SimpleDataSource("jdbc:mysql://localhost:3306/ding_cloud_push?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8",
                "demo", "demo");
        Db db = Db.use(ds);
        Entity entity = db.get(Entity.create("open_sync_biz_data")
                .set("corp_id", "ding9614b8451b0891b0a1320dcb25e91351").set("biz_type", "4"));

        String biz_data = entity.getStr("biz_data");
        JSONObject jsonObject = JSONUtil.parseObj(biz_data);
        String suiteTicket = jsonObject.getStr("suiteTicket");*/

        /*DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/service/get_corp_token");
        OapiServiceGetCorpTokenRequest req = new OapiServiceGetCorpTokenRequest();
        req.setAuthCorpid("ding9614b8451b0891b0a1320dcb25e91351");
        OapiServiceGetCorpTokenResponse execute = client.execute(req,
                "suitebrsjv0xmhmypgrim","viuEoioSuRdF3WxZGEQfb7skON5nuW8knO_5JgcGHgaEBWj_2prQJfccNS-Q9vS6",
                "");

        client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/edu/campus/list");
        OapiEduCampusListRequest req1 = new OapiEduCampusListRequest();
        OapiEduCampusListResponse rsp = client.execute(req1,execute.getAccessToken());
        System.out.println(rsp.getBody());*/

//        listCampus();

        DefaultDingTalkClient  client = new DefaultDingTalkClient("https://oapi.dingtalk.com/microapp/list");
        OapiMicroappListRequest req = new OapiMicroappListRequest();
        OapiMicroappListResponse response = client.execute(req,AccessTokenUtil.getToken());
        List<OapiMicroappListResponse.Applist> appList = response.getAppList();
        for (OapiMicroappListResponse.Applist app : appList) {
            String homepageLink = app.getHomepageLink();
            String pcHomepageLink = app.getPcHomepageLink();
            System.out.println(app.getName() +"  "+pcHomepageLink);
        }
        System.out.println();
    }

    /**
     *  班级信息:
     *      classId:
     *      classAchiveId:
     *      className:"一年级2班"
     *      classNick:""
     *      campusGradeAchiveId:""
     *      grade:
     *      subjectCount: 课程数量
     *
     *  老师信息:
     *      teachId:
     *      teachName:
     *
     *  课程信息:
     *      subjectId:
     *      subjectName:"数学"
     *      classSubjectId：
     *
     *  学年学期:
     *      beginDate
     *      endDate
     *      name
     *      isCurrent:是否为当前学期
     *
     *  课表信息:
     *      id
     *      classId
     *      classAchiveId
     *      className
     *      classNick
     *      campusGradeAchiveId
     *      grade
     *      publishState
     *      semesterYear
     *      semesterNo
     *      updateUserName
     *
     *
     *  班级—>老师->课程
     *     class_teacher 班级关联老师
     *     class_teacher_subject 老师关联课程
     *
     *
     *  作息时间->作息时间明细
     *     class_time_table 作息时间
     *      id:
     *      name:
     *      enabled:
     *
     *     class_time_table_item 作息时间明细
     *      id
     *      class_time_table_id
     *      timePeriod: 时段 1=上午 2=中午 3=下午 4=晚上
     *      isClassTime: 是否为上课时间 false=不是 true=是
     *      name: 名称
     *      beginTime: 起始时间 按照分钟存储 00:01 = 1
     *      endTime: 结束时间 按照分钟存储 01:00 = 60
     *
     *  课程表 -> 周一(weekDay)、时间段(timePeriod)、第一节课、上课时间(每周 单周 双周 单双周)、课程(subjectId)、老师(teachers)
     *  单双周 = 每周
     *
     *    class_time_table
     *        class_id
     *             is_publish:是否发布 0=未发布 1=已发布
     *
     *     class_time_table_detail
     *        subject_id:课程id
     *        subject_name:课程名称
     *        seq: 第几节课 // 删除作息时间第一个 自动跑到第2节课了
     *        week_day: 周几
     *        time_period: 时段 1=上午 2=中午 3=下午 4=晚上
     *        coord_type: 0=必修 1=选修
     *        coord_name: 名称
     *        teachers: 老师id 逗号隔开
     *    // api/ClassTimetable/GetClassTimetableDetail
     *
     *    // 调课审核通过后，老师课表就不显示了
     *
     *    // left join 调课记录 或者 代课记录
     *
     *   // 自习老师可以选择老师
     *
     *   // 三个老师一节课 其中一个老师申请调课
     *
     *   // 审核通过后,最后一级审批人可以修改调课
     *
     *
     *  新课标起始周
     */

    /**
     * 支持 自定义 班级任教老师 不受钉钉家校通讯录的限制
     * 家长随时随地查看孩子周期课表，再也不用担心提前备课
     */

}
